<?php

namespace app\api\service;

use app\common\model\NoticeModel;
use app\common\model\order\OrderGoodsModel;
use app\common\model\order\OrderModel;
use app\common\model\user\UserCardModel;

class OrderService
{
    public static function order_find($type = 0)
    {
        if ($type == 0) return ['id,order_no,goods_count,order_amount,express,express_no,pay_status,status'];
        if ($type == 1) return ['id,order_no,goods_count,express_amount,discount_amount,goods_amount,order_amount,express,
        express_no,user_name,user_mobile,user_adds,remark,ship_time,confirm_time,create_time,pay_status,card_price,status'];
    }

    public static function handle_order($uid)
    {
        $list = OrderModel::where('user_id', $uid)->where('pay_status', 0)->whereIn('status', 1)
            ->field('id,create_time')->select()->toArray();
        foreach ($list as $item) {
            if (time() >= strtotime($item['create_time']) + 1740) {
                OrderModel::where('id', $item['id'])->update(['status' => 0]);
            }
        }
        return true;
    }

    public static function order_notice($order)
    {
        if ($order) {
            $content = '';
            $goodsDiv = '';
            $goodsList = OrderGoodsModel::where('order_id', $order['id'])->select();
            for ($i = 0; $i < count($goodsList); $i++) {
                $div = '<div style="width: 100%;display: flex;align-items: center;padding-bottom: 10px;">
					<img src=' . $goodsList[$i]['product_img'] . ' style="width: 68px;height: 68px;border-radius:4px;" />
					<div style="width: calc(100% - 78px);margin-left:10px;">
						<div style="font-size: 12px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">
                           ' . $goodsList[$i]['goods_name'] . '
						</div>
						<div style="font-size: 12px;color: #999;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">
                           ' . $goodsList[$i]['product_name'] . '
						</div>
						<div style="display: flex;justify-content: space-between;margin-top: 8px;">
							<div style="font-size: 11px;">×' . $goodsList[$i]['quantity'] . ' </div>
                            <div style="font-size: 13px;">¥' . $goodsList[$i]['product_price'] . '</div>
						</div>
					</div>
				    </div>';
                $goodsDiv = $goodsDiv . $div;
            };
            $goods = '<div style="margin-top: 10px;background: #f8f8f8;padding:10px 10px 0;border-radius:4px;color: #000000;">' . $goodsDiv . '</div>';
            $content = $content . $goods;
            $adds = '<div style="margin-top: 8px;">姓名：' . $order['user_name'] . '</div>
              <div>电话：' . $order['user_mobile'] . '</div>
              <div>地址：' . $order['user_adds'] . '</div>
              <h4 style="color: #FA5150;margin-top: 8px;font-size: 12px;">请核对以上信息，如需修改请联系在线客服，我们会尽快为您发货。</h4>';
            $content = $content . $adds;
            (new NoticeModel())->send($order['user_id'], '我们已经收到您的订单啦！ 🎉', $content, 0, '', '/pages/order/details?id=' . $order['id'], 3);
        }
    }


    public static function return_card($oid)
    {
        $ucId = OrderModel::where('id', $oid)->value('user_card_id');
        if ($ucId) UserCardModel::where('id', $ucId)->update(['status' => 1]);
        return $ucId;
    }

    public static function status_str($pay_status, $status)
    {
        if ($pay_status == 0 && $status == 1) {
            $str = '待支付（30m内有效）';
        } else if ($status == 2) {
            $str = '待发货';
        } else if ($status == 3) {
            $str = '待收货';
        } else if ($status == 4) {
            $str = '待评价';
        } else if ($status == 5) {
            $str = '已完成';
        } else if ($status == 6) {
            $str = '已取消';
        } else if ($pay_status == 2 && $status == 2) {
            $str = '待发货（售后中）';
        } else if ($pay_status == 2 && $status == 3) {
            $str = '待收货（售后中）';
        } else if ($pay_status == 2 && $status == 4) {
            $str = '待评价（售后中）';
        } else if ($pay_status == 2 && $status == 5) {
            $str = '已完成（售后中）';
        } else if ($pay_status == 3 && $status == 2) {
            $str = '待发货（部分退款）';
        } else if ($pay_status == 3 && $status == 3) {
            $str = '待收货（部分退款）';
        } else if ($pay_status == 3 && $status == 4) {
            $str = '待评价（部分退款）';
        } else if ($pay_status == 3 && $status == 5) {
            $str = '已完成（部分退款）';
        } else if ($pay_status == 4) {
            $str = '已退款';
        } else if ($pay_status == 0 || $status == 0) {
            $str = '已取消';
        }
        return $str ?? '';
    }
}
